package best.driver.android.Utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.util.ArrayMap;
import best.driver.android.BestApp;
import best.driver.android.R;
import com.nostra13.universalimageloader.utils.L;

import java.util.ArrayList;


public class VoiceUtils {

    private static MediaPlayer sMediaPlayer;

    public static boolean playBadNetworkAlarm(boolean loop) {
        Uri uri = Uri.parse(
                "android.resource://" + BestApp.getInstance().getPackageName() + "/" + R.raw.alert_new_order);
        final int streamType = getStreamType(RingtoneManager.TYPE_ALARM);
        return playSound(streamType, uri, loop);
    }

    private static int getStreamType(int type) {
        if (RingtoneManager.TYPE_ALARM == type) {
            return AudioManager.STREAM_ALARM;
        } else if (RingtoneManager.TYPE_RINGTONE == type) {
            return AudioManager.STREAM_RING;
        } else if (RingtoneManager.TYPE_NOTIFICATION == type) {
            return AudioManager.STREAM_NOTIFICATION;
        }
        return AudioManager.STREAM_RING;
    }

    private static boolean playSound(final int streamType, final Uri uri, Boolean loop) {
        try {
            L.d("playSound.1 try to play sound:%s", uri);
            if (uri == null) {
                return false;
            }

            if (sMediaPlayer != null) {
                try {
                    if (sMediaPlayer.isPlaying()) {
                        sMediaPlayer.stop();
                    }
                } catch (Exception e) {
                    L.d("playSound.2 e:%s", e.toString());
                    e.printStackTrace();
                }

                try {
                    sMediaPlayer.reset();
                    sMediaPlayer.release();
                    sMediaPlayer = null;
                } catch (Exception e) {
                    L.d("playSound.3 e:%s", e.toString());
                    e.printStackTrace();
                }
            }

            sMediaPlayer = new MediaPlayer();
            final AudioManager audioManager = (AudioManager) BestApp.getInstance()
                    .getSystemService(
                            Context.AUDIO_SERVICE);
            final int volume = audioManager.getStreamVolume(streamType);
            L.d("playSound.4 save audio vol = " + volume);
            audioManager.setStreamVolume(streamType, audioManager.getStreamMaxVolume(streamType), 0);
            //sStreamType = streamType;

            sMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                    sMediaPlayer = null;
                    audioManager.setStreamVolume(streamType, volume, 0);
                }
            });
            sMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mp.release();
                    sMediaPlayer = null;
                    L.e("playSound.5 Error in MediaPlayer: (" + what + ") with extra (" + extra + ")");
                    audioManager.setStreamVolume(streamType, volume, 0);
                    return false;
                }
            });
            sMediaPlayer.setAudioStreamType(streamType);
            sMediaPlayer.setLooping(loop);
            try {
                sMediaPlayer.setDataSource(BestApp.getInstance(), uri);
            } catch (Exception e) {
                L.d("playSound.6 e:%s", e.toString());
                e.printStackTrace();
                return false;
            }
            try {
                sMediaPlayer.prepare();
            } catch (Exception e) {
                L.d("playSound.7 e:%s", e.toString());
                e.printStackTrace();
                return false;
            }
            sMediaPlayer.start();
            return true;
        } catch (IllegalStateException e) {
            L.d("playSound.8 e:%s", e.toString());
            e.printStackTrace();
        }
        return false;
    }
}
